﻿using PLCS.App.Shared.Events;
using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.Service.Dialog;

namespace PLCS.App.Shared.ViewModels;

public class NavigationViewModel : BaseViewModel, INavigationAware
{
    public new readonly IHostDialogService dialog;
    public readonly IEventAggregator aggregator;

    public NavigationViewModel()
    {
        dialog = ContainerLocator.Container.Resolve<IHostDialogService>();
        aggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
    }

    #region INavigationAware

    /// <summary>
    /// 导航目标是否重新利用原来的对象
    /// </summary>
    /// <param name="navigationContext"></param>
    /// <returns></returns>
    public virtual bool IsNavigationTarget(NavigationContext navigationContext) => true;

    /// <summary>
    /// 导航发生变更时
    /// </summary>
    /// <param name="navigationContext"></param>
    public virtual void OnNavigatedFrom(NavigationContext navigationContext)
    { }

    /// <summary>
    /// 导航到达时
    /// </summary>
    /// <param name="navigationContext"></param>
    public async void OnNavigatedTo(NavigationContext navigationContext)
    {
        await OnNavigatedToAsync(navigationContext);
    }

    #endregion INavigationAware

    /// <summary>
    /// 导航到达时的异步方法
    /// </summary>
    /// <param name="navigationContext"></param>
    /// <returns></returns>
    public virtual async Task OnNavigatedToAsync(NavigationContext navigationContext)
    {
        await RefreshAsync();
    }

    /// <summary>
    /// 异步刷新方法,当页面导航到达时触发该方法
    /// </summary>
    /// <returns></returns>
    public virtual async Task RefreshAsync() => await Task.CompletedTask;

    #region 加载动画

    public void UpdateLoading(bool IsOpen)
    {
        aggregator.UpdateLoading(new UpdateModel()
        {
            IsOpen = IsOpen
        });
    }

    #endregion 加载动画
}